//
//  BarcodeInputCell.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/5.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}



protocol HDBarcodeInputCellInputDelegate:NSObjectProtocol {
    func clicKReturn(_ cell: HDBarcodeInputCell)
}

class HDBarcodeInputCell: HDFounctionBaseCell , UITextFieldDelegate , AVQRCodeViewManagerInputDelegate , HDInputViewStateProtocol , CaptuvoDecoderManagerDelegate , HDgetAndSetProtocol{
    //MARK: - property
    override var info: [String : String]?{
        didSet{
            self.codeInputView.textField.text = info!["text"]
        }
    }
    weak var InputDelegate:HDBarcodeInputCellInputDelegate?{
        didSet{
            print(InputDelegate!)
        }
    }
    lazy var codeInputView: WmInputView = {
        let view = WmInputView()  
        view.textField.delegate = self
        return view
    }()
    var inputState:inputViewState = inputViewState.disabled{
        didSet{
            DispatchQueue.main.async { 
                switch self.inputState { //改变状态
                case .activated:
                    self.codeInputView.isUserInteractionEnabled = true
//                    self.codeInputView.textField.inputView = customTextfield.emptyView()
                    self.codeInputView.textField.resignFirstResponder()
                    if self.codeInputView.textField.canBecomeFirstResponder{
                        self.codeInputView.textField.becomeFirstResponder()
                    }
                    AVQRCodeViewManager.shareAVQRCodeViewManager.InputDelegate = self
                    CaptuvoDecoderManager.shareCaptuvoDecoderManager.inputDelegate = self
                    self.updateButtonState()
                case .disabled:
                    self.codeInputView.isUserInteractionEnabled = false
                    self.codeInputView.button_camera.isHidden = true
                    self.codeInputView.button_clear.isHidden = true
                case .wrong:
                    self.codeInputView.isUserInteractionEnabled = true
                    self.codeInputView.textField.text = nil
                    self.updateButtonState()
//                    self.codeInputView.button_camera.hidden = false
//                    self.codeInputView.button_clear.hidden = true
                    self.codeInputView.textField.becomeFirstResponder()
                }
                
            }
    }
    }
    var state:inputViewState?{
        get{
            return inputState
        }
        set{
            inputState = newValue!
        }
        
        
    }

    //MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUI()
    }
    
    required init() {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "BarcodeInputCell")
        self.setUpUI()
    }
    //MARK: - delegate
    func getText() -> (String?) {
        return self.codeInputView.textField.text
    }
    func SetText(_ text: String?) {
        self.codeInputView.textField.text = text
    }
    func finishScan(_ msg: String?) {
        if self.InputDelegate != nil {
            self.InputDelegate?.clicKReturn(self)
        }
    }
    func finishCapScan(_ msg: String?) {
        if self.InputDelegate != nil {
            self.InputDelegate?.clicKReturn(self)
        }
    }
    //MARK: - public
    override func clearContent() {
        self.codeInputView.textField.text = ""
    }
    //MARK: - textFieldDelegate
//    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
//        self.updateButtonState()
//        return true
//    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if self.InputDelegate != nil {
            self.InputDelegate?.clicKReturn(self)
        }
        return true
    }
    //MARK: - private
    func updateButtonState(){  //手动更改文字的时候手动调用此方法，否则button可能显示不正常
        if captuvoIsOn {
            codeInputView.button_camera.isHidden = true
            codeInputView.button_clear.isHidden = codeInputView.textField.text?.characters.count > 0 ? false : true
            return
        }
        codeInputView.button_camera.isHidden = codeInputView.textField.text?.characters.count > 0 ? true : false
        codeInputView.button_clear.isHidden = codeInputView.textField.text?.characters.count > 0 ? false : true
    }
    func setUpUI(){
         self.codeInputView.textField.addTarget(self, action: #selector(HDKeyboardInputCell.updateButtonState), for: UIControlEvents.editingChanged)
        self.state = inputViewState.disabled
        self.addSubview(self.codeInputView)
        codeInputView.snp.makeConstraints { (make) in
            make.leading.equalTo(self).offset(inputViewToCellSpace)
            make.top.equalTo(self).offset(FTCellTopSpace+3)
            make.bottom.equalTo(self).offset(-FTCellTopSpace-3)
            make.trailing.equalTo(self).offset(-9)
        }
    }
    
    
}
